package com.revenant.driver.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.nanchen.compresshelper.CompressHelper;
import com.revenant.driver.R;
import com.revenant.driver.base.BaseActivity;
import com.revenant.driver.bean.Driver.CarManageListBean;
import com.revenant.driver.bean.Driver.ColorAndEnergerBean;
import com.revenant.driver.bean.ShipperBean.FileUploadBean;
import com.revenant.driver.utils.GlideEngine;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.revenant.driver.utils.Constants.Driver_Car_Color;
import static com.revenant.driver.utils.Constants.UploadFile;

public class DriverIAddVetticalActivity extends BaseActivity {
    @BindView(R.id.color_slect)
    NiceSpinner colorSlect;
    @BindView(R.id.energe_slect)
    NiceSpinner energeSlect;
    @BindView(R.id.upload_text_type)
    TextView uploadTextType;
    @BindView(R.id.upload_second_title)
    TextView uploadSecondTitle;
    @BindView(R.id.upload_photo)
    ImageView uploadPhoto;
    @BindView(R.id.upload_type)
    Button uploadType;
    private Context context;

    private String uploadurl = "";
    private List<LocalMedia> selectList = new ArrayList<>();
    List<String> color = new ArrayList<>();
    List<String> colorCode = new ArrayList<>();
    List<String> energe = new ArrayList<>();
    List<String> energeCode = new ArrayList<>();
    private String colorCodeText = "";
    private String energeCodeText = "";
    private String cartypeid = "-1";


    @Override
    public int getContentViewResId() {
        return R.layout.activity_driver_add_vertical;
    }

    @Override
    public void initView() {
        setcenterTitle("添加车辆");
        context = this;
        cartypeid=getIntent().getStringExtra("cartypeid");
        getColor();

    }

    @Override
    public void leftbarclick() {
        super.leftbarclick();
        finish();
    }

    @Override
    public void initData() {
        colorSlect.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                colorCodeText = colorCode.get(position);
            }
        });

        energeSlect.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                energeCodeText = energeCode.get(position);
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.upload_photo, R.id.upload_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.upload_photo:
                showBottomSheetDialog(DriverIAddVetticalActivity.this);
//                PictureSelector.create(this)
//                        .openGallery(PictureMimeType.ofImage())
//                        .maxSelectNum(1)
//                        .loadImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
//                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.upload_type:
                if (uploadurl.isEmpty()) {
                    showToast("请选择图片");
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("imageurlone", uploadurl);
                    bundle.putString("cartypeid", cartypeid);
                    bundle.putString("colorCode", colorCodeText);
                    bundle.putString("energeCode", energeCodeText);
                    startActivity(DriverIAddVetticalActivityTwo.class, bundle);
                    finish();
                }

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    selectList = PictureSelector.obtainMultipleResult(data);
                    for (LocalMedia media : selectList) {
                        if (!selectList.isEmpty()) {
                            String url = media.getPath().startsWith("content://") ? media.getAndroidQToPath() : media.getPath();
                            uploadAvatar(url);
                        }
                    }
                    break;
            }
        }
    }


    private void uploadAvatar(final String path) {
        showDig();
        File file = new File(path);
        File newfile = CompressHelper.getDefault(context).compressToFile(file);

        OkGo.<String>post(UploadFile)
                .tag(this)
                .isMultipart(true)

                .params("file", newfile)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        dismissDig();
                        if (response.code() == 200) {
                            FileUploadBean fileUploadBean = GsonUtil.parseJsonWithGson(response.body(), FileUploadBean.class);
                            if (fileUploadBean.getCode().equals("0")) {
                                uploadurl = fileUploadBean.getData().getUrl();
                                Glide.with(context).load(fileUploadBean.getData().getUrl()).centerCrop().
                                        into(uploadPhoto);
                            } else {
                                showToast(fileUploadBean.getMsg());
                            }
                        } else {
                            showToast(response.message());
                        }


                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissDig();
                        showToast(response.message());
                    }
                });
    }

    private void getColor() {

        OkGo.<String>get(Driver_Car_Color)

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ColorAndEnergerBean colorAndEnergerBean = GsonUtil.parseJsonWithGson(response.body(), ColorAndEnergerBean.class);
                        if (colorAndEnergerBean.getCode().equals("0")) {
                            if (colorAndEnergerBean.getData().getColList() != null && colorAndEnergerBean.getData().getColList().size() > 0) {
                                for (int i = 0; i < colorAndEnergerBean.getData().getColList().size(); i++) {
                                    color.add(colorAndEnergerBean.getData().getColList().get(i).getName());
                                    colorCode.add(colorAndEnergerBean.getData().getColList().get(i).getCode());
                                    energe.add(colorAndEnergerBean.getData().getEnList().get(i).getName());
                                    energeCode.add(colorAndEnergerBean.getData().getEnList().get(i).getCode());
                                }

                                colorSlect.attachDataSource(color);
                                energeSlect.attachDataSource(energe);
                                colorSlect.setSelectedIndex(0);
                                energeSlect.setSelectedIndex(0);
                                colorCodeText = colorCode.get(0);
                                energeCodeText = energeCode.get(0);
                            }

                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }
}
